﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThietBiSo.Models;
using ThietBiSo.LinQDB;

namespace ThietBiSo.Controllers
{
    public class bannerController : Controller
    {
        //
        // GET: /banner/

        private MBanner mbn = new MBanner();
        Banner bn = new Banner();
        
        public ActionResult Index()
        {
            ViewData["banners"] = mbn.FindAll();
            return View();
        }
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {


                bn.Id = mbn.GetMax() + 1;
                bn.MaLoaiQuangCao = int.Parse(collection.Get("ddlLoai"));
                bn.UrlHinh = collection.Get("HinhAnh");
                bn.HienThi = true;
                mbn.Save(bn);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Edit(int id)
        {
            bn = mbn.GetByID(id);
            ViewData["banners"] = bn;
            return View();
        }

        //
        // POST: /SanPham/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
               
                bn.Id = id;

                if (collection.Get("HinhAnh") == null || collection.Get("HinhAnh") == "")
                {
                    bn.UrlHinh = collection.Get("HiddenHinhAnh");
                }
                else
                {
                    bn.UrlHinh = collection.Get("HinhAnh");
                }
                bn.MaLoaiQuangCao = int.Parse(collection.Get("ddlLoai"));
                mbn.Update(bn);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
